/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package xinyu.customer.network;

import com.logos.common.source.network.NetWork;
import com.logos.common.source.network.callback.HttpRespondResultCallBack;
import com.logos.common.source.network.callback.MultiUploadFileProgressListener;
import com.logos.tool.ApplicationUtil;
import com.logos.tool.device.DeviceUtils;
import com.logos.tool.device.MacAddressUtil;
import com.logos.tool.device.VersionUtils;
import com.logos.tool.string.StringUtil;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import xinyu.customer.BuildConfig;
import xinyu.customer.JGApplication;
import xinyu.customer.util.APKUtil;
import xinyu.customer.util.SpConstant;


/**
 * @author Young Ken
 */

public class AppNetWork {

    /**
     * post
     *
     * @param baseUrl  baseUrl
     * @param url      url
     * @param param    param
     * @param header   消息头
     * @param callBack 回调
     */
    public static void post(String baseUrl, String url,
                            Map<String, Object> param, Map<String, String> header,
                            HttpRespondResultCallBack callBack) {

        NetWork.Builder builder = new NetWork.Builder()
                .baseUrl(getBaseUrl(baseUrl))
                .url(url)
                .param(param)
                .cache(false)
                .header(getHeader(header))
                .callBack(callBack);
        builder.build().post(builder);
    }

    /**
     * post
     *
     * @param url      url
     * @param param    param
     * @param header   消息头
     * @param callBack 回调
     */
    public static void post(String url,
                            Map<String, Object> param, Map<String, String> header,
                            HttpRespondResultCallBack callBack) {
        post(null, url, param, header, callBack);
    }

    /**
     * post
     *
     * @param url      url
     * @param param    param
     * @param callBack 回调
     */
    public static void post(String url,
                            Map<String, Object> param,
                            HttpRespondResultCallBack callBack) {
        post(null, url, param, null, callBack);
    }

    /**
     * get
     *
     * @param baseUrl  baseUrl
     * @param url      url
     * @param param    param
     * @param header   消息头
     * @param callBack 回调
     */
    public static void get(String baseUrl, String url,
                           Map<String, Object> param, Map<String, String> header,
                           HttpRespondResultCallBack callBack) {

        NetWork.Builder builder = new NetWork.Builder()
                .baseUrl(getBaseUrl(baseUrl))
                .url(url)
                .param(param)
                .cache(false)
                .header(getHeader(header))
                .callBack(callBack);
        builder.build().get(builder);
    }

    /**
     * get
     *
     * @param url      url
     * @param param    param
     * @param header   消息头
     * @param callBack 回调
     */
    public static void get(String url,
                           Map<String, Object> param, Map<String, String> header,
                           HttpRespondResultCallBack callBack) {
        get(null, url, param, header, callBack);
    }

    /**
     * get
     *
     * @param url      url
     * @param param    param
     * @param callBack 回调
     */
    public static void get(String url,
                           Map<String, Object> param,
                           HttpRespondResultCallBack callBack) {
        get(null, url, param, null, callBack);
    }

    /**
     * get
     *
     * @param url      url
     * @param callBack 回调
     */
    public static void get(String url, HttpRespondResultCallBack callBack) {
        get(url, null, null, callBack);
    }

    /**
     * upload
     *
     * @param baseUrl  baseUrl
     * @param url      url
     * @param fileMap  文件map
     * @param param    表单map
     * @param header   消息头
     * @param callBack 回调
     * @param listener UploadFileProgressListener
     */
    public static void upload(String baseUrl, String url,
                              Map<String, File> fileMap,
                              Map<String, Object> param,
                              Map<String, String> header,
                              HttpRespondResultCallBack callBack,
                              MultiUploadFileProgressListener listener) {

        NetWork.Builder builder = new NetWork.Builder()
                .baseUrl(getBaseUrl(baseUrl))
                .url(url)
                .param(param)
                .cache(false)
                .header(getHeader(header))
                .callBack(callBack)
                .files(fileMap)
                .addMultiUploadFileProgressListener(listener);
        builder.build().upload(builder);
    }

    /**
     * upload
     *
     * @param url      url
     * @param fileMap  文件map
     * @param param    表单map
     * @param callBack 回调
     */
    public static void upload(String url, Map<String, File> fileMap,
                              Map<String, Object> param,
                              HttpRespondResultCallBack callBack) {
        upload(null, url, fileMap, param, null, callBack, null);
    }

    /**
     * upload
     *
     * @param url      url
     * @param fileMap  文件map
     * @param param    表单map
     * @param callBack 回调
     * @param listener UploadFileProgressListener
     */
    public static void upload(String url, Map<String, File> fileMap,
                              Map<String, Object> param,
                              HttpRespondResultCallBack callBack,
                              MultiUploadFileProgressListener listener) {
        upload(null, url, fileMap, param, null, callBack, listener);
    }

    /**
     * upload
     *
     * @param url      url
     * @param fileMap  文件map
     * @param callBack 回调
     */
    public static void upload(String url, Map<String, File> fileMap,
                              HttpRespondResultCallBack callBack) {

        upload(null, url, fileMap, null, null, callBack, null);
    }

    /**
     * upload
     *
     * @param url      url
     * @param fileMap  文件map
     * @param callBack 回调
     * @param listener UploadFileProgressListener
     */
    public static void upload(String url, Map<String, File> fileMap,
                              HttpRespondResultCallBack callBack,
                              MultiUploadFileProgressListener listener) {

        upload(null, url, fileMap, null, null, callBack, listener);
    }

    /**
     * upload
     *
     * @param url      url
     * @param fileMap  文件map
     * @param param    表单map
     * @param header   消息头
     * @param callBack 回调
     */
    public static void upload(String url, Map<String, File> fileMap,
                              Map<String, Object> param, Map<String, String> header,
                              HttpRespondResultCallBack callBack) {

        upload(null, url, fileMap, param, header, callBack, null);
    }

    /**
     * upload
     *
     * @param url      url
     * @param fileMap  文件map
     * @param param    表单map
     * @param header   消息头
     * @param callBack 回调
     * @param listener UploadFileProgressListener
     */
    public static void upload(String url, Map<String, File> fileMap,
                              Map<String, Object> param, Map<String, String> header,
                              HttpRespondResultCallBack callBack,
                              MultiUploadFileProgressListener listener) {

        upload(null, url, fileMap, param, header, callBack, listener);
    }

    /**
     * delete
     *
     * @param baseUrl  baseUrl
     * @param url      url
     * @param param    表单map
     * @param header   消息头
     * @param callBack 回调
     */
    public static void delete(String baseUrl, String url,
                              Map<String, Object> param, Map<String, String> header,
                              HttpRespondResultCallBack callBack) {
        NetWork.Builder builder = new NetWork.Builder()
                .baseUrl(getBaseUrl(baseUrl))
                .url(url)
                .param(param)
                .cache(false)
                .header(getHeader(header))
                .callBack(callBack);
        builder.build().delete(builder);
    }

    /**
     * delete
     *
     * @param url      url
     * @param callBack 回调
     */
    public static void delete(String url, HttpRespondResultCallBack callBack) {
        delete(null, url, null, null, callBack);
    }

    /**
     * put方法
     *
     * @param baseUrl  baseUrl
     * @param url      url
     * @param param    param
     * @param header   header
     * @param callBack callBack
     */
    public static void put(String baseUrl, String url,
                           Map<String, Object> param, Map<String, String> header,
                           HttpRespondResultCallBack callBack) {
        NetWork.Builder builder = new NetWork.Builder()
                .baseUrl(getBaseUrl(baseUrl))
                .url(url)
                .param(param)
                .cache(false)
                .header(getHeader(header))
                .callBack(callBack);
        builder.build().put(builder);
    }

    /**
     * put方法
     *
     * @param url      url
     * @param param    param
     * @param callBack callBack
     */
    public static void put(String url,
                           Map<String, Object> param,
                           HttpRespondResultCallBack callBack) {
        put(null, url, param, null, callBack);
    }

    private static String getBaseUrl(String baseUrl) {
        if (baseUrl == null) {
            baseUrl = BuildConfig.ANT_BASE_URL;
        }
        return baseUrl;
    }

    /**
     * model-type	是	string	手机型号
     * channel	是	string	分发渠道
     * app-key	是	string	应用key
     * version	是	string	版本
     * longitude	否	string	经度
     * latitude	否	string	维度
     * city	否	string	城市
     * access-token	否	string	登陆标识
     * sign-token	是	string	签名
     * ip	否	string	ip地址
     * mac-address	是	string	mac地址
     * noncestr	是	string	随机字符串（32位，字符数字组合）
     *
     * @param header
     * @return
     */
    private static Map getHeader(Map header) {
        if (header == null) {
            header = new HashMap();
        }
        String refway =  APKUtil.getMetaDataValue(JGApplication.getContext(),"UMENG_CHANNEL");
        header.put("plat", "1");
        header.put("Connection", "close");
        header.put("version", BuildConfig.VERSION_NAME);
        header.put("Cookie","sek=" + SpConstant.getSek() + ";" + "cust_id=" + SpConstant.getUserId() + ";plat=1;version=" + BuildConfig.VERSION_NAME+";refway="+refway);

        return header;
    }
}
